package com.u.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


/**
 * Servlet implementation class UploadFile
 */
@WebServlet("/UploadFile")
public class UploadFile extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String filePath = "store";
	private String tempFilePath = "temp";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadFile() {
        super();
        // TODO Auto-generated constructor stub
    }
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");
		filePath = getServletContext().getRealPath("store");
    	tempFilePath = getServletContext().getRealPath("temp");
		PrintWriter outNet = response.getWriter();
		try {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			factory.setSizeThreshold(16*1024);
			System.out.println(tempFilePath);
			factory.setRepository(new File(tempFilePath));
			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setSizeMax(16*1024*1024);
			List items = upload.parseRequest(request);
			Iterator iter = items.iterator();
			String filename = "";
			while(iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				filename = processUploadFile(item);
			}
			RequestDispatcher requestDispatcher;
			String method = "uploadUserList";
			request.setAttribute("method", "uploadUserList");
			request.setAttribute("filename", filename);
			requestDispatcher = request.getRequestDispatcher("/SuperRootServlet");
			requestDispatcher.forward(request, response);
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
	
	private String processUploadFile(FileItem item) throws Exception {
		String filename = item.getName();
		int index = filename.lastIndexOf("\\");
		filename = filename.substring(index+1);
		long fileSize = item.getSize();
		if(filename.equals("") && fileSize == 0) return null;
		
		File uploadedFile = new File(filePath + "/" + filename);
		item.write(uploadedFile);
		return filePath + "/" + filename;
	}

}
